package com.alu.array;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

/**
 * ysun13@ebay.com
 * 
 * @author Administrator
 *
 */
public class Solution {
	
	/**
	 * 
	 * @param numbers unsorted array
	 * @param target
	 * @return
	 */
	public int[] twoSum(int[] numbers, int target) {
		
		int[] result = new int[]{-1,-1};
		
		// Key --- target-current   Value --- current index
		Map<Integer, Integer> map = new HashMap<>();
		loop:for(int i=0; i<numbers.length; i++) {
			if(map.containsKey(numbers[i])) {
				result[0] = map.get(numbers[i]); // first one -- the stored index
				result[1] = i; // second one -- current index
				break loop;
			} else {
				map.put(target - numbers[i], i);
			}
		}
		
		return result;
    }
	
	@Test
	public void test(){
		int[] arr = {6,5,3,4,2,1};
		int[] result = this.twoSum(arr, 7);
		assert 1 == result[0];
		assert 5 == result[1];
		
		result = this.twoSum(arr, 20);
		assert -1 == result[0];
		assert -1 == result[1];
		
		result = this.twoSum(arr, -1);
		assert -1 == result[0];
		assert -1 == result[1];
		
		int[] arr2 = {-6,-5,-3,-4,-2,-1};
		result = this.twoSum(arr2, -7);
		assert 0 == result[0];
		assert 5 == result[1];
		
		result = this.twoSum(arr2, 0);
		assert -1 == result[0];
		assert -1 == result[1];
	}
	
}
